<template>
  <el-form :model="ruleForm" ref="ruleForm" :label-position="labelPosition"
           :class="isEdit?'':'formItem'"
           :rules="rules" :inline="false">

    <el-card class="box-card" shadow="never">
      <div slot="header" class="header-card">
        <span class="header-card-title">退保信息（商业险信息）</span>
      </div>

      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="退保申请日期:" prop="refundBeginTime">
            <el-input v-if="isEdit" v-model="ruleForm.refundBeginTime" disabled></el-input>
            <span v-else>{{ ruleForm.refundBeginTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商业险保单号:" prop="syxPolicyCode">
            <el-input v-if="isEdit" v-model="ruleForm.syxPolicyCode" disabled></el-input>
            <span v-else>{{ ruleForm.syxPolicyCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="保险公司:" prop="syxCompany">
            <el-input v-if="isEdit" v-model="ruleForm.syxCompany" disabled></el-input>
            <span v-else>{{ ruleForm.syxCompany }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="保险到期日:" prop="syxActualEndTime">
            <el-input v-if="isEdit" v-model="ruleForm.syxActualEndTime" disabled></el-input>
            <span v-else>{{ ruleForm.syxActualEndTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="总费用:" prop="totalAmount">
            <el-input v-thousand v-if="isEdit"
                      v-model="ruleForm.totalAmount" disabled></el-input>
            <span v-else>{{ format_thousand(ruleForm.totalAmount) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退保保费:" prop="refundInsuranceAmount">
            <el-input v-model="ruleForm.refundInsuranceAmount" placeholder="请输入退保保费"
                      v-thousand
                      @change="changeAmount('refundInsuranceAmount')"
                      v-if="isEdit"></el-input>
            <span v-else>{{ format_thousand(ruleForm.refundInsuranceAmount) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退保生效日期:" prop="refundInsuranceTime">
            <el-date-picker v-model="ruleForm.refundInsuranceTime" type="date" placeholder="请选择退保生效日期"
                            v-if="isEdit"
                            value-format='yyyy-MM-dd' style="width: 100%;">
            </el-date-picker>
            <span v-else>{{ ruleForm.refundInsuranceTime }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="附件:" prop="attachmentUrl">
            <image-common v-model="ruleForm.attachmentUrl" :limit="20" :disabled="true" :upType="2"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退保批单:" prop="batchOrderOss">
            <image-common v-model="ruleForm.batchOrderOss" :limit="20" :disabled="!isEdit" :upType="2"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-card>
  </el-form>
</template>

<script>
export default {
  name: "cancellationInfo", //退保信息（商业险信息）
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    },
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            if (value) {
              that.handleClearValidate(key)
            }
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === 'write') {
        return 'top'
      } else {
        return 'right'
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    }
  },
  data() {
    var amount = (rule, value, callback) => {
      let that = this
      if (value === '') {
        callback(new Error('请输入退保保费'));
      } else {
        if (value > Number(that.value.totalAmount)) {
          callback(new Error('退保保费大于总费用'));
        } else {
          callback();
        }
      }
    }
    return {
      rules: {
        refundBeginTime: [{required: this.state === 'write'}],
        syxPolicyCode: [{required: this.state === 'write'}],
        syxCompany: [{required: this.state === 'write'}],
        syxActualEndTime: [{required: this.state === 'write'}],
        totalAmount: [{required: this.state === 'write'}],
        refundInsuranceAmount: [{required: this.state === 'write', validator: amount, trigger: "blur"}],
        refundInsuranceTime: [{required: this.state === 'write', message: "请选择退保生效日期", trigger: "blur"}],
        attachmentUrl: [
          {required: this.state === 'write', message: "请上传附件", trigger: "blur"},
        ],
        batchOrderOss: [
          {required: this.state === 'write', message: "请上传附件", trigger: "blur"},
        ]
      },
    };
  },
  created() {
    this.value.refundBeginTime = this.handleTime()
  },
  mounted() {
  },
  methods: {

    valid() {
      return new Promise((resolve) => {
        this.$refs.ruleForm.validate((valid) => {
          resolve(valid);
        });
      });
    },

    handleClearValidate(str) {
      this.$refs.ruleForm.clearValidate(str);
    },

    changeAmount(str) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(this.ruleForm[str])) {
        this.ruleForm[str] = "";
      } else {
        this.ruleForm[str] = this.ruleForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }

    },

    handleTime() {
      const now = new Date();
      const year = now.getFullYear();
      const month = this.padStart(now.getMonth() + 1);
      const day = this.padStart(now.getDate());
      const hours = this.padStart(now.getHours());
      const minutes = this.padStart(now.getMinutes());
      const seconds = this.padStart(now.getSeconds());
      // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      return `${year}-${month}-${day}`;
    },

    padStart(value) {
      return value.toString().padStart(2, '0');
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";

.elForm {
  .el-form-item {
    width: 100%;
  }
}
</style>


